<!DOCTYPE html>
<html>
	{include file="public1/_header" /}
	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="">首页</a>
				<a>
					<cite>数据库备份</cite></a>
			</span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()"
			 title="刷新">
				<i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
			</a>
		</div>
		<div class="layui-fluid">
		  <div class="layui-row layui-col-space15">
		    <div class="layui-col-md12">
		      <div class="layui-card">
		        <div class="layui-card-body">
		          <table class="layui-hide" id="test" lay-filter="test"></table>
		        </div>
		      </div>
		    </div>
		  </div>
		</div> 
		
		
		 
		<script type="text/html" id="toolbarDemo">
		  <div class="layui-btn-container">
		    <button class="layui-btn layui-btn-sm" lay-event="getCheckData">备份</button>
		    <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">还原</button>
		    <button class="layui-btn layui-btn-sm" lay-event="isAll">修复</button>
		  </div>
		</script>
	</body>
	<script type="text/javascript">
		function renderSize(value){
		    if(null==value||value==''){
		        return "0 B";
		    }
		    var unitArr = new Array("B","KB","MB","GB","TB","PB","EB","ZB","YB");
		    var index=0,
		        srcsize = parseFloat(value);
		 index=Math.floor(Math.log(srcsize)/Math.log(1024));
		    var size =srcsize/Math.pow(1024,index);
		    //  保留的小数位数
		    size=size.toFixed(2);
		    return size+unitArr[index];
		}
			$(function(){
				layui.use('table', function() {
					var table = layui.table;
				
					table.render({
						elem: '#test',
						url: "{:url('lists')}",
						toolbar: '#toolbarDemo',
						title: '用户数据表',
						cols: [
							[{
								type: 'checkbox',
								fixed: 'left',
								width:80
							}, {
								field: 'name',
								title: '表名'
							}, {
								field: 'data_length',
								title: '数据大小',
								templet:function(d){
									return renderSize(d.data_length);
								}
							}, {
								field: 'engine',
								title: '引擎'

							}, {
								field: 'create_time',
								title: '创建时间'
							}]
						]
					});
				
					//头工具栏事件
					table.on('toolbar(test)', function(obj) {
						var checkStatus = table.checkStatus(obj.config.id);
						switch (obj.event) {
							case 'getCheckData':
								var data = checkStatus.data;
								if(data.length<1){
									layer.msg('你必须选中一张表进行备份');
									return false
								}
								var tables = [];
								data.forEach(function(item,index){
									tables.push(item.name)
								})
								var ntables = tables
								$.post("{:url('backups')}",{data:ntables},function(res){
									if(res.status == 0){
										layer.msg(res.message, {
										  icon: 1,
										  time: 500 //2秒关闭（如果不配置，默认是3秒）
										}, function(){
											location.reload()
										});
									}
								})
								break;
							case 'getCheckLength':
								parent.layer.open({
									title: "数据备份列表",
									type: 2,
									area: ['900px', '600px'],
									content: "{:url('datalist')}"
								});
								break;
							case 'isAll':
								var data = checkStatus.data;
								if(data.length<1){
									layer.msg('你必须选中一张表进行修复');
									return false
								}
								var tables = [];
								data.forEach(function(item,index){
									tables.push(item.name)
								})
								var ntables = tables
								$.post("{:url('repair')}",{data:ntables},function(res){
									if(res.status == 0){
										layer.msg(res.message, {
										  icon: 1,
										  time: 500 //2秒关闭（如果不配置，默认是3秒）
										}, function(){
											location.reload()
										});
									}
								})
								break;
						};
					});
				
					//监听行工具事件
					table.on('tool(test)', function(obj) {
						var data = obj.data;
						//console.log(obj)
						if (obj.event === 'del') {
							layer.confirm('真的删除行么', function(index) {
								obj.del();
								layer.close(index);
							});
						} else if (obj.event === 'edit') {
							layer.prompt({
								formType: 2,
								value: data.email
							}, function(value, index) {
								obj.update({
									email: value
								});
								layer.close(index);
							});
						}
					});
				});
			})
	</script>
</html>
